merge.cons <- subset(merge.cons,merge.cons$session!=3)#
#
#get rid of other party members#
merge.cons$name <- factor(merge.cons$name)
merge.cons$support
merge.cons
names(merge.cons)
for(j in levels(merge.cons$name)){ #change to number of files#
#
x <- subset(merge.cons,merge.cons$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("conservative")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
cons.loyalty[order(cons.loyalty$loyalty),]
x <- subset(merge.cons,merge.cons$name=="Flaherty Jim")
x
Flaherty Jim
x <- subset(merge.cons,merge.cons$name=="Flaherty Jim")
z
x
lib.margin$party.position <- ifelse(lib.margin$yay > lib.margin$nay,1,ifelse(lib.margin$yay < lib.margin$nay,0,NA))#
merge.lib <- merge(lib,lib.margin,"vote.id")#
#
merge.lib$support <- ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 1, 1,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 0, 1,ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 0, 0,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.lib <- subset(merge.lib,merge.lib$session!=3)#
#
#get rid of other party members#
merge.lib$name <- factor(merge.lib$name)
1.2 Liberal#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Liberal')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("liberal")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
liberal.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(liberal.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.3 NDP#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='NDP')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("ndp")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#select three parties for analysis#
#
cons <- subset(dd,dd$party=='Conservative')#
lib <- subset(dd,dd$party=='Liberal')#
ndp <- subset(dd,dd$party=='NDP')
3.1 NDP#
#
#match individual MP with party position#
#
ndp.margin$party.position <- ifelse(ndp.margin$yay > ndp.margin$nay,1,ifelse(ndp.margin$yay < ndp.margin$nay,0,NA))#
merge.ndp <- merge(ndp,ndp.margin,"vote.id")#
#
merge.ndp$support <- ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 1, 1,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 0, 1,ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 0, 0,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.ndp <- subset(merge.ndp,merge.ndp$session!=3)#
#
#get rid of other party members#
merge.ndp$name <- factor(merge.ndp$name)#
#
#start individual loop#
for(j in levels(merge.ndp$name)){ #change to number of files#
#
x <- subset(merge.ndp,merge.ndp$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("conservative")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
ndp.loyalty[order(ndp.loyalty$loyalty),]#
#
#Rice index ndp#
#
ndp.margin$rice <- abs(ndp.margin$yay - ndp.margin$nay)/(ndp.margin$yay + ndp.margin$nay)
ndp.margin$party.position <- ifelse(ndp.margin$yay > ndp.margin$nay,1,ifelse(ndp.margin$yay < ndp.margin$nay,0,NA))#
merge.ndp <- merge(ndp,ndp.margin,"vote.id")#
#
merge.ndp$support <- ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 1, 1,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 0, 1,ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 0, 0,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.ndp <- subset(merge.ndp,merge.ndp$session!=3)#
#
#get rid of other party members#
merge.ndp$name <- factor(merge.ndp$name)#
#
#start individual loop#
for(j in levels(merge.ndp$name)){ #change to number of files#
#
x <- subset(merge.ndp,merge.ndp$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("ndp")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
ndp.loyalty[order(ndp.loyalty$loyalty),]#
#
#Rice index ndp#
#
ndp.margin$rice <- abs(ndp.margin$yay - ndp.margin$nay)/(ndp.margin$yay + ndp.margin$nay)
library(foreign)#
data1 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")#
data2 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
data <- rbind(data1,data2)#
#
#Add party info to data#
party <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-party.csv")#
#
data$name <- paste(data$last.name,data$first.name)#
party$name <- paste(party$Last.Name,party$First.Name)#
#
pp <- data.frame(party$name,party$Political.Affiliation)#
colnames(pp) <- c("name","party")#
merge <- merge(data,pp,by.x="name")#
#
vote <- merge#
#
#Change the date format#
#
vote$date <- as.character(vote$date)#
x <- read.table(text=as.character(vote$date), sep=' ', fill=TRUE, as.is=TRUE)#
vote$date.vote <- x$V1#
#
#Get rid of party switchers#
#
#extract members#
vote$name <- factor(vote$name)#
dd <- data.frame(vote)#
dd$date.vote <- as.Date(dd$date.vote, "%m/%d/%Y")#
#
##2011.12.05--2013-06-09 Goldring Peter Independent Conservative (from Conservative)#
#2013.06.10 Goldring Peter, Conservative#
#
dd$party1 <- ifelse(dd$date.vote < as.Date("2011-12-05") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2011-12-04") & dd$date.vote < as.Date("2013-06-09") & dd$name=="Goldring Peter","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-09") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
#
#2012.01.10 St-Denis, Lise Liberal (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-01-10") & dd$name=="St-Denis Lise","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-01-09") & dd$name=="St-Denis Lise","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2012.04.23 Hyer, Bruse, Independent (from NDP)#
#2013.12.13 Hyer, Bruce, Green Party (from Independent)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-04-23") & dd$name=="Hyer Bruce","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-04-22") & dd$date.vote < as.Date("2013-12-13") & dd$name=="Hyer Bruce","Independent","NA")#
dd$party <- ifelse(dd$party1=="Indepenent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-12-12") & dd$name=="Hyer Bruce","Green Party","NA")#
dd$party <- ifelse(dd$party1=="Green Party","Green Party",as.character(dd$party))#
#
#2013.02.28 Patry, Claude, Bloc Quebecois (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-02-28") & dd$name=="Patry Claude","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-02-27") & dd$name=="Patry Claude","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
#
#2013.06.06 Rathberger, Brent, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-06-06") & dd$name=="Rathberger Brent","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-05") & dd$name=="Rathberger Brent","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.12 Mourani, Maria, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-12") & dd$name=="Mourani Maria","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-11") & dd$name=="Mourani Maria","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.30 Del Mastro, Dean, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-30") & dd$name=="Del Mastro Dean","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-29") & dd$name=="Del Mastro Dean","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.06.06 Perreault, Manon, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-06-06") & dd$name=="Perreault Manon","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-06-05") & dd$name=="Perreault Manon","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.12-2014.10.20 Fortin, Jean-François, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-12") & dd$name=="Fortin Jean-Fran\xe7ois","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-11") & dd$date.vote < as.Date("2013-10-21") & dd$name=="Fortin Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.20 Hassainia, Sana, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-20") & dd$name=="Hassainia Sana","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-19") & dd$name=="Hassainia Sana","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.25 Bellavance, André, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-25") & dd$name=="Bellavance Andr\xe9","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-24") & dd$name=="Bellavance Andr\xe9","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.10.21 Larose, Jean-François, Force et Démocratie (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-10-21") & dd$name=="Larose Jean-Fran\xe7ois","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-10-20") & dd$name=="Larose Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Pacetti, Massimo, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Pacetti Massimo","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Pacetti Massimo","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Andrews, Scott, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Andrews Scott","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Andrews Scott","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.01.05 Thibeault, Glenn, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-01-05") & dd$name=="Thibeault Glenn","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-01-04") & dd$name=="Thibeault Glenn","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.02.09 Adams, Eve, Liberal (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-02-09") & dd$name=="Adams Eve","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-02-08") & dd$name=="Adams Eve","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2015.03.31 Lunney, James, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-03-30") & dd$name=="Lunney James","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-03-30") & dd$name=="Lunney James","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.05.10 - 2013.08.26 Marc Harb (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-05-10") & dd$name=="Harb Marc","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-05-09") & dd$name=="Harb Marc","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#Compute individual vote#
#
vote <- dd#
#
vote$vote.c <- ifelse(vote$vote=='Nay',0,ifelse(vote$vote=='Yea',1,ifelse(vote$vote=='Yea/Nay',99,ifelse(vote$vote=='Paired',8,NA))))#
#
#Create vote id variable#
#
vote$vote.id <-  paste(vote$session,vote$vote.no,sep = "v")#
#
vote$vote.id <- paste("s",vote$vote.id,sep = "")#
#
vote$vote.id <- factor(vote$vote.id)#
#
dd <- data.frame(vote)#
#
#Get rid of duplicates#
d <- dd[,-2]#
de <- d[,-17]#
df <- unique(de)#
#
####################################################
#1. loop for majority support in all three parties##
####################################################
#
#1.1 Conservative#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Conservative')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("conservative")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.2 Liberal#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Liberal')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("liberal")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
liberal.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(liberal.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.3 NDP#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='NDP')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("ndp")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#select three parties for analysis#
#
cons <- subset(dd,dd$party=='Conservative')#
lib <- subset(dd,dd$party=='Liberal')#
ndp <- subset(dd,dd$party=='NDP')#
#
##############################################
#2. loop individual mp to get loyalty scores##
##############################################
#
#2.1 Conservative#
#
#match individual MP with party position#
#
cons.margin$party.position <- ifelse(cons.margin$yay > cons.margin$nay,1,ifelse(cons.margin$yay < cons.margin$nay,0,NA))#
merge.cons <- merge(cons,cons.margin,"vote.id")#
#
merge.cons$support <- ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 1, 1,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 0, 1,ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 0, 0,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.cons <- subset(merge.cons,merge.cons$session!=3)#
#
#get rid of other party members#
merge.cons$name <- factor(merge.cons$name)#
#
#start individual loop#
for(j in levels(merge.cons$name)){ #change to number of files#
#
x <- subset(merge.cons,merge.cons$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("conservative")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
cons.loyalty[order(cons.loyalty$loyalty),]#
#
#Rice index conservative#
#
cons.margin$rice <- abs(cons.margin$yay - cons.margin$nay)/(cons.margin$yay + cons.margin$nay)#
#
#2.1 Liberal#
#
#match individual MP with party position#
#
lib.margin$party.position <- ifelse(lib.margin$yay > lib.margin$nay,1,ifelse(lib.margin$yay < lib.margin$nay,0,NA))#
merge.lib <- merge(lib,lib.margin,"vote.id")#
#
merge.lib$support <- ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 1, 1,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 0, 1,ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 0, 0,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.lib <- subset(merge.lib,merge.lib$session!=3)#
#
#get rid of other party members#
merge.lib$name <- factor(merge.lib$name)#
#
#start individual loop#
for(j in levels(merge.lib$name)){ #change to number of files#
#
x <- subset(merge.lib,merge.lib$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("liberal")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
lib.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(lib.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
lib.loyalty[order(lib.loyalty$loyalty),]#
#
#Rice index liberal#
#
lib.margin$rice <- abs(lib.margin$yay - lib.margin$nay)/(lib.margin$yay + lib.margin$nay)#
#
#3.1 NDP#
#
#match individual MP with party position#
#
ndp.margin$party.position <- ifelse(ndp.margin$yay > ndp.margin$nay,1,ifelse(ndp.margin$yay < ndp.margin$nay,0,NA))#
merge.ndp <- merge(ndp,ndp.margin,"vote.id")#
#
merge.ndp$support <- ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 1, 1,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 0, 1,ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 0, 0,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.ndp <- subset(merge.ndp,merge.ndp$session!=3)#
#
#get rid of other party members#
merge.ndp$name <- factor(merge.ndp$name)#
#
#start individual loop#
for(j in levels(merge.ndp$name)){ #change to number of files#
#
x <- subset(merge.ndp,merge.ndp$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("ndp")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
ndp.loyalty[order(ndp.loyalty$loyalty),]#
#
#Rice index ndp#
#
ndp.margin$rice <- abs(ndp.margin$yay - ndp.margin$nay)/(ndp.margin$yay + ndp.margin$nay)
lib.margin$party.position <- ifelse(lib.margin$yay > lib.margin$nay,1,ifelse(lib.margin$yay < lib.margin$nay,0,NA))#
merge.lib <- merge(lib,lib.margin,"vote.id")
lib.margin <- liberal.margin
lib.margin$party.position <- ifelse(lib.margin$yay > lib.margin$nay,1,ifelse(lib.margin$yay < lib.margin$nay,0,NA))#
merge.lib <- merge(lib,lib.margin,"vote.id")
merge.lib$support <- ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 1, 1,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 0, 1,ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 0, 0,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.lib <- subset(merge.lib,merge.lib$session!=3)#
#
#get rid of other party members#
merge.lib$name <- factor(merge.lib$name)#
#
#start individual loop#
for(j in levels(merge.lib$name)){ #change to number of files#
#
x <- subset(merge.lib,merge.lib$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("liberal")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
lib.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(lib.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
lib.loyalty[order(lib.loyalty$loyalty),]
match individual MP with party position#
#
ndp.margin$party.position <- ifelse(ndp.margin$yay > ndp.margin$nay,1,ifelse(ndp.margin$yay < ndp.margin$nay,0,NA))#
merge.ndp <- merge(ndp,ndp.margin,"vote.id")#
#
merge.ndp$support <- ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 1, 1,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 0, 1,ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 0, 0,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.ndp <- subset(merge.ndp,merge.ndp$session!=3)#
#
#get rid of other party members#
merge.ndp$name <- factor(merge.ndp$name)#
#
#start individual loop#
for(j in levels(merge.ndp$name)){ #change to number of files#
#
x <- subset(merge.ndp,merge.ndp$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("ndp")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
ndp.loyalty[order(ndp.loyalty$loyalty),]#
#
#Rice index ndp#
#
ndp.margin$rice <- abs(ndp.margin$yay - ndp.margin$nay)/(ndp.margin$yay + ndp.margin$nay)
x <- subset(merge.lib,merge.lib$name=="Simm Scott")
x
x <- subset(merge.lib,merge.lib$name=="Simms Scott")
x
cons.margin$rice <- abs(cons.margin$yay - cons.margin$nay)/(cons.margin$yay + cons.margin$nay)
cons.margin$rice
cons.margin
cons.margin[order(.margin$rice),]
cons.margin[order(cons.margin$rice),]
x <- subset(df,df$party=='Conservative')
x
names(x)
xx <- subset(x,x$vote.id=="s1v466")
xx
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-06-06") & dd$name=="Rathberger Brent","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))
dd$party
names(dd)
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-06-06") & dd$name=="Rathberger Brent","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-05") & dd$name=="Rathberger Brent","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))
x <- subset(dd,dd$name=="Rathberger Brent")
x
dd$name
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s1/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s1//Alain-Giguere(425).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}
x
xx
x[,10:15]
f
x <- rbind(x,xx)
names(x)
names(xx)
read.csv(file="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//K-Kellie-Leitch(71786).csv", header=F)
s <0 read.csv(file="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//K-Kellie-Leitch(71786).csv", header=F)
s <- read.csv(file="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//K-Kellie-Leitch(71786).csv", header=F)
names(s)
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s1/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s1//Alain-Giguere(425).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")
x
names(x)
names(xx)
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}
x
names(x)
names(xx)
xx[1]
xx[1,1]
xx[1,]
names(xx)
names(x)
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}
x
names(x)
names(xx)
f
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
#replace K. Kellie Leitch
xs <- subset(x,x$last.name=="Leich")
xs
xs <- subset(x,x$last.name=="Leitch")
xs
xxx$first.name <- ifelse(x$first.name=="Khristinn Kellie","Kellie",ifelse(x$firstname=="K. Kellie","Kellie",x$first.name))
xxx <- ifelse(x$first.name=="Khristinn Kellie","Kellie",ifelse(x$firstname=="K. Kellie","Kellie",x$first.name))
xxx
?sub
x$first.name["Khristinn Kellie"]
x$first.name[x$first.name=="Khristinn Kellie"]
x$first.name[x$first.name=="Khristinn Kellie"] <- "Kellie"
x$first.name[x$first.name=="K. Kellie"] <- "Kellie"
ddd <- sub(x,x$last.name=="Leitch")
names(x)
x
ddd <- sub(x,x$last.name=="Leitch")
x$last.name
xs <- subset(x,x$last.name=="Leich")
xs
xs <- subset(x,x$last.name=="Leitch")
xs
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
#replace K. Kellie Leitch#
#
x$first.name[x$first.name=="Khristinn Kellie"] <- "Kellie"#
x$first.name[x$first.name=="K. Kellie"] <- "Kellie"#
#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s1/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s1//Alain-Giguere(425).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")
library(foreign)#
data1 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")#
data2 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
data <- rbind(data1,data2)#
#
#Add party info to data#
party <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-party.csv")#
#
data$name <- paste(data$last.name,data$first.name)#
party$name <- paste(party$Last.Name,party$First.Name)#
#
pp <- data.frame(party$name,party$Political.Affiliation)#
colnames(pp) <- c("name","party")#
merge <- merge(data,pp,by.x="name")#
#
vote <- merge#
#
#Change the date format#
#
vote$date <- as.character(vote$date)#
x <- read.table(text=as.character(vote$date), sep=' ', fill=TRUE, as.is=TRUE)#
vote$date.vote <- x$V1#
#
#Get rid of party switchers#
#
#extract members#
vote$name <- factor(vote$name)#
dd <- data.frame(vote)#
dd$date.vote <- as.Date(dd$date.vote, "%m/%d/%Y")#
#
##2011.12.05--2013-06-09 Goldring Peter Independent Conservative (from Conservative)#
#2013.06.10 Goldring Peter, Conservative#
#
dd$party1 <- ifelse(dd$date.vote < as.Date("2011-12-05") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2011-12-04") & dd$date.vote < as.Date("2013-06-09") & dd$name=="Goldring Peter","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-09") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
#
#2012.01.10 St-Denis, Lise Liberal (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-01-10") & dd$name=="St-Denis Lise","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-01-09") & dd$name=="St-Denis Lise","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2012.04.23 Hyer, Bruse, Independent (from NDP)#
#2013.12.13 Hyer, Bruce, Green Party (from Independent)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-04-23") & dd$name=="Hyer Bruce","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-04-22") & dd$date.vote < as.Date("2013-12-13") & dd$name=="Hyer Bruce","Independent","NA")#
dd$party <- ifelse(dd$party1=="Indepenent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-12-12") & dd$name=="Hyer Bruce","Green Party","NA")#
dd$party <- ifelse(dd$party1=="Green Party","Green Party",as.character(dd$party))#
#
#2013.02.28 Patry, Claude, Bloc Quebecois (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-02-28") & dd$name=="Patry Claude","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-02-27") & dd$name=="Patry Claude","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
#
#2013.06.06 Rathberger, Brent, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-06-06") & dd$name=="Rathgeber Brent","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-05") & dd$name=="Rathgeber Brent","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.12 Mourani, Maria, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-12") & dd$name=="Mourani Maria","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-11") & dd$name=="Mourani Maria","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.30 Del Mastro, Dean, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-30") & dd$name=="Del Mastro Dean","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-29") & dd$name=="Del Mastro Dean","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.06.06 Perreault, Manon, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-06-06") & dd$name=="Perreault Manon","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-06-05") & dd$name=="Perreault Manon","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.12-2014.10.20 Fortin, Jean-François, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-12") & dd$name=="Fortin Jean-Fran\xe7ois","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-11") & dd$date.vote < as.Date("2013-10-21") & dd$name=="Fortin Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.20 Hassainia, Sana, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-20") & dd$name=="Hassainia Sana","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-19") & dd$name=="Hassainia Sana","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.25 Bellavance, André, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-25") & dd$name=="Bellavance Andr\xe9","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-24") & dd$name=="Bellavance Andr\xe9","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.10.21 Larose, Jean-François, Force et Démocratie (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-10-21") & dd$name=="Larose Jean-Fran\xe7ois","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-10-20") & dd$name=="Larose Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Pacetti, Massimo, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Pacetti Massimo","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Pacetti Massimo","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Andrews, Scott, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Andrews Scott","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Andrews Scott","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.01.05 Thibeault, Glenn, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-01-05") & dd$name=="Thibeault Glenn","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-01-04") & dd$name=="Thibeault Glenn","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.02.09 Adams, Eve, Liberal (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-02-09") & dd$name=="Adams Eve","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-02-08") & dd$name=="Adams Eve","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2015.03.31 Lunney, James, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-03-30") & dd$name=="Lunney James","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-03-30") & dd$name=="Lunney James","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.05.10 - 2013.08.26 Marc Harb (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-05-10") & dd$name=="Harb Marc","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-05-09") & dd$name=="Harb Marc","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#Compute individual vote#
#
vote <- dd#
#
vote$vote.c <- ifelse(vote$vote=='Nay',0,ifelse(vote$vote=='Yea',1,ifelse(vote$vote=='Yea/Nay',99,ifelse(vote$vote=='Paired',8,NA))))#
#
#Create vote id variable#
#
vote$vote.id <-  paste(vote$session,vote$vote.no,sep = "v")#
#
vote$vote.id <- paste("s",vote$vote.id,sep = "")#
#
vote$vote.id <- factor(vote$vote.id)#
#
dd <- data.frame(vote)#
#
#Get rid of duplicates#
d <- dd[,-2]#
de <- d[,-17]#
df <- unique(de)#
#
####################################################
#1. loop for majority support in all three parties##
####################################################
#
#1.1 Conservative#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Conservative')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("conservative")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.2 Liberal#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Liberal')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("liberal")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
liberal.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(liberal.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.3 NDP#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='NDP')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("ndp")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#select three parties for analysis#
#
cons <- subset(dd,dd$party=='Conservative')#
lib <- subset(dd,dd$party=='Liberal')#
ndp <- subset(dd,dd$party=='NDP')#
#
##############################################
#2. loop individual mp to get loyalty scores##
##############################################
#
#2.1 Conservative#
#
#match individual MP with party position#
#
cons.margin$party.position <- ifelse(cons.margin$yay > cons.margin$nay,1,ifelse(cons.margin$yay < cons.margin$nay,0,NA))#
merge.cons <- merge(cons,cons.margin,"vote.id")#
#
merge.cons$support <- ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 1, 1,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 0, 1,ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 0, 0,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.cons <- subset(merge.cons,merge.cons$session!=3)#
#
#get rid of other party members#
merge.cons$name <- factor(merge.cons$name)#
#
#start individual loop#
for(j in levels(merge.cons$name)){ #change to number of files#
#
x <- subset(merge.cons,merge.cons$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("conservative")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
cons.loyalty[order(cons.loyalty$loyalty),]#
#
#Rice index conservative#
#
cons.margin$rice <- abs(cons.margin$yay - cons.margin$nay)/(cons.margin$yay + cons.margin$nay)#
cons.margin[order(cons.margin$rice),]
x <- subset(cons.margin,cons.margin$vote.id==s1v466)
names(cons.margin)
names(merge.cons$name)
names(merge.cons)
x <- subset(merge.cons,merge.cons$vote.id==s1v466)
x <- subset(merge.cons,merge.cons$vote.id=="s1v466")
x
x <- subset(merge.cons,merge.cons$vote.id=="s1v466")
x
names(x)
length(x$vote.id)
x$name
x[order(x$name),]
x$name[order(x$name),]
zz<-x$[order(x$name),]
zz<-x[order(x$name),]
zz$name
x
zz$name
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv", recursive=TRUE)#
#
library(foreign)#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s2/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s2//Adam-Vaughan(54434).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
#replace K. Kellie Leitch#
#
x$first.name[x$first.name=="Khristinn Kellie"] <- "Kellie"#
x$first.name[x$first.name=="K. Kellie"] <- "Kellie"#
#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
#
FileNames <- list.files(path="~/Dropbox/Canadian-House/House-41/MP-vote-s1/", full.names=TRUE)#
for(f in FileNames){ #
  data <- read.csv(file=f, header=F)#
#
if (f=="/Users/godboutj/Dropbox/Canadian-House/House-41/MP-vote-s1//Alain-Giguere(425).csv")#
#
  x <- data[-1,]#
else #
{#
 xx <- data[-1,]#
 x <- rbind(x,xx)#
}#
}#
#
names(x)<-c("parliament","session","date","vote.result","vote.no","vote.subject","yeas","nays","paired","hon","first.name","last.name","constituency","province","vote")#
#
x$first.name[x$first.name=="Khristinn Kellie"] <- "Kellie"#
x$first.name[x$first.name=="K. Kellie"] <- "Kellie"#
#
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")
library(foreign)#
data1 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")#
data2 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
data <- rbind(data1,data2)#
#
#Add party info to data#
party <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-party.csv")#
#
data$name <- paste(data$last.name,data$first.name)#
party$name <- paste(party$Last.Name,party$First.Name)#
#
pp <- data.frame(party$name,party$Political.Affiliation)#
colnames(pp) <- c("name","party")#
merge <- merge(data,pp,by.x="name")#
#
vote <- merge#
#
#Change the date format#
#
vote$date <- as.character(vote$date)#
x <- read.table(text=as.character(vote$date), sep=' ', fill=TRUE, as.is=TRUE)#
vote$date.vote <- x$V1#
#
#Get rid of party switchers#
#
#extract members#
vote$name <- factor(vote$name)#
dd <- data.frame(vote)#
dd$date.vote <- as.Date(dd$date.vote, "%m/%d/%Y")#
#
##2011.12.05--2013-06-09 Goldring Peter Independent Conservative (from Conservative)#
#2013.06.10 Goldring Peter, Conservative#
#
dd$party1 <- ifelse(dd$date.vote < as.Date("2011-12-05") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2011-12-04") & dd$date.vote < as.Date("2013-06-09") & dd$name=="Goldring Peter","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-09") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
#
#2012.01.10 St-Denis, Lise Liberal (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-01-10") & dd$name=="St-Denis Lise","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-01-09") & dd$name=="St-Denis Lise","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2012.04.23 Hyer, Bruse, Independent (from NDP)#
#2013.12.13 Hyer, Bruce, Green Party (from Independent)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-04-23") & dd$name=="Hyer Bruce","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-04-22") & dd$date.vote < as.Date("2013-12-13") & dd$name=="Hyer Bruce","Independent","NA")#
dd$party <- ifelse(dd$party1=="Indepenent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-12-12") & dd$name=="Hyer Bruce","Green Party","NA")#
dd$party <- ifelse(dd$party1=="Green Party","Green Party",as.character(dd$party))#
#
#2013.02.28 Patry, Claude, Bloc Quebecois (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-02-28") & dd$name=="Patry Claude","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-02-27") & dd$name=="Patry Claude","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
#
#2013.06.06 Rathberger, Brent, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-06-06") & dd$name=="Rathgeber Brent","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-05") & dd$name=="Rathgeber Brent","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.12 Mourani, Maria, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-12") & dd$name=="Mourani Maria","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-11") & dd$name=="Mourani Maria","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.30 Del Mastro, Dean, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-30") & dd$name=="Del Mastro Dean","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-29") & dd$name=="Del Mastro Dean","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.06.06 Perreault, Manon, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-06-06") & dd$name=="Perreault Manon","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-06-05") & dd$name=="Perreault Manon","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.12-2014.10.20 Fortin, Jean-François, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-12") & dd$name=="Fortin Jean-Fran\xe7ois","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-11") & dd$date.vote < as.Date("2013-10-21") & dd$name=="Fortin Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.20 Hassainia, Sana, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-20") & dd$name=="Hassainia Sana","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-19") & dd$name=="Hassainia Sana","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.25 Bellavance, André, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-25") & dd$name=="Bellavance Andr\xe9","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-24") & dd$name=="Bellavance Andr\xe9","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.10.21 Larose, Jean-François, Force et Démocratie (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-10-21") & dd$name=="Larose Jean-Fran\xe7ois","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-10-20") & dd$name=="Larose Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Pacetti, Massimo, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Pacetti Massimo","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Pacetti Massimo","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Andrews, Scott, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Andrews Scott","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Andrews Scott","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.01.05 Thibeault, Glenn, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-01-05") & dd$name=="Thibeault Glenn","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-01-04") & dd$name=="Thibeault Glenn","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.02.09 Adams, Eve, Liberal (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-02-09") & dd$name=="Adams Eve","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-02-08") & dd$name=="Adams Eve","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2015.03.31 Lunney, James, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-03-30") & dd$name=="Lunney James","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-03-30") & dd$name=="Lunney James","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.05.10 - 2013.08.26 Marc Harb (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-05-10") & dd$name=="Harb Marc","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-05-09") & dd$name=="Harb Marc","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#Compute individual vote#
#
vote <- dd#
#
vote$vote.c <- ifelse(vote$vote=='Nay',0,ifelse(vote$vote=='Yea',1,ifelse(vote$vote=='Yea/Nay',99,ifelse(vote$vote=='Paired',8,NA))))#
#
#Create vote id variable#
#
vote$vote.id <-  paste(vote$session,vote$vote.no,sep = "v")#
#
vote$vote.id <- paste("s",vote$vote.id,sep = "")#
#
vote$vote.id <- factor(vote$vote.id)#
#
dd <- data.frame(vote)#
#
#Get rid of duplicates#
d <- dd[,-2]#
de <- d[,-17]#
df <- unique(de)#
#
####################################################
#1. loop for majority support in all three parties##
####################################################
#
#1.1 Conservative#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Conservative')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("conservative")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.2 Liberal#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Liberal')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("liberal")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
liberal.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(liberal.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.3 NDP#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='NDP')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("ndp")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#select three parties for analysis#
#
cons <- subset(dd,dd$party=='Conservative')#
lib <- subset(dd,dd$party=='Liberal')#
ndp <- subset(dd,dd$party=='NDP')#
#
##############################################
#2. loop individual mp to get loyalty scores##
##############################################
#
#2.1 Conservative#
#
#match individual MP with party position#
#
cons.margin$party.position <- ifelse(cons.margin$yay > cons.margin$nay,1,ifelse(cons.margin$yay < cons.margin$nay,0,NA))#
merge.cons <- merge(cons,cons.margin,"vote.id")#
#
merge.cons$support <- ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 1, 1,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 0, 1,ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 0, 0,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.cons <- subset(merge.cons,merge.cons$session!=3)#
#
#get rid of other party members#
merge.cons$name <- factor(merge.cons$name)#
#
#start individual loop#
for(j in levels(merge.cons$name)){ #change to number of files#
#
x <- subset(merge.cons,merge.cons$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("conservative")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
cons.loyalty[order(cons.loyalty$loyalty),]#
#
#Rice index conservative#
#
cons.margin$rice <- abs(cons.margin$yay - cons.margin$nay)/(cons.margin$yay + cons.margin$nay)#
cons.margin[order(cons.margin$rice),]
match individual MP with party position#
#
lib.margin <- liberal.margin#
#
lib.margin$party.position <- ifelse(lib.margin$yay > lib.margin$nay,1,ifelse(lib.margin$yay < lib.margin$nay,0,NA))#
merge.lib <- merge(lib,lib.margin,"vote.id")#
#
merge.lib$support <- ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 1, 1,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 0, 1,ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 0, 0,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.lib <- subset(merge.lib,merge.lib$session!=3)#
#
#get rid of other party members#
merge.lib$name <- factor(merge.lib$name)#
#
#start individual loop#
for(j in levels(merge.lib$name)){ #change to number of files#
#
x <- subset(merge.lib,merge.lib$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("liberal")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
lib.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(lib.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
lib.loyalty[order(lib.loyalty$loyalty),]
lib.margin$rice <- abs(lib.margin$yay - lib.margin$nay)/(lib.margin$yay + lib.margin$nay)
lib.margin[order(lib.margin$rice),]
match individual MP with party position#
#
ndp.margin$party.position <- ifelse(ndp.margin$yay > ndp.margin$nay,1,ifelse(ndp.margin$yay < ndp.margin$nay,0,NA))#
merge.ndp <- merge(ndp,ndp.margin,"vote.id")#
#
merge.ndp$support <- ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 1, 1,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 0, 1,ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 0, 0,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.ndp <- subset(merge.ndp,merge.ndp$session!=3)#
#
#get rid of other party members#
merge.ndp$name <- factor(merge.ndp$name)#
#
#start individual loop#
for(j in levels(merge.ndp$name)){ #change to number of files#
#
x <- subset(merge.ndp,merge.ndp$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("ndp")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
ndp.loyalty[order(ndp.loyalty$loyalty),]#
#
#Rice index ndp
ndp.margin$rice <- abs(ndp.margin$yay - ndp.margin$nay)/(ndp.margin$yay + ndp.margin$nay)#
ndp.margin[order(ndp.margin$rice),]
cons.margin$rice <- abs(cons.margin$yay - cons.margin$nay)/(cons.margin$yay + cons.margin$nay)#
cons.margin[order(cons.margin$rice),]
x <- cons.loyalty[order(cons.loyalty$loyalty),]
x
write.csv(x,file = "~/Dropbox/Canadian-House/House-41/data/MP-41-Cons.csv")
rm(list=ls())#
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl);library(arm);library(car)#
counter <- 1:40#
parls <- c(paste("Can",counter,sep=""))#
data(list=parls)#
terms <- list(Can1,Can2,Can3,Can4,Can5,Can6,Can7,Can8,#
              Can9,Can10,Can11,Can12,Can13,Can14,Can15,Can16,#
              Can17,Can18,Can19,Can20,Can21,Can22,Can23,Can24,#
              Can25,Can26,Can27,Can28,Can29,Can30,Can31,Can32,#
              Can33,Can34,Can35,Can36,Can37,Can38,Can39,Can40)
govinfo <- read.csv("../../Canada_Cohesion/Data/govinfo.csv",header=F)#
colnames(govinfo) <- c("Parliament","govparty","govtype","total.MP")#
out <- loyaltydata("Liberal",terms,counter)# all
out
?loyaltydata
?loyalty
allloy <- loyalty(Can40)
allloy
?CanParl
allloy <- loyalty(Can39)
allloy
CANADA HOUSE OF COMMONS LOYALTY + VOTE INFO MASTER FILE##
##########################################################
#
#THINGS TO DO#
#LOOP OVER ALL FILES (USE 1-34 old loop file).#
#Merge with vote information file.#
#DELETE FILES WHEN RUNNING CODE AGAIN#
#
file.remove("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.txt", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/Analysis/Vote-1-40.txt", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.csv", recursive=TRUE)#
file.remove("~/Dropbox/Canadian-House/Analysis/Vote-1-40.csv", recursive=TRUE) #
#
rm(list=ls())#
library(foreign);library(oc);library(pscl);library(wnominate);library(car);#
FileNames <- list.files(path="~/Dropbox/Canadian-House/Parliaments/", full.names=TRUE) #CHANGE FOLDER FOR NOW#
#
#Loop Files#
#
for(f in FileNames){ #
data <- read.csv(file=f, header=T)#
if (f=="/Users/godboutj/Dropbox/Canadian-House/Parliaments//House-01.csv")#
{  #
 parlouze <- 1#
}#
else #
{#
 parlouze <- parlouze + 1#
}#
#
vote.info <- data[1:3,29:dim(data)[2]]#
vote.info <-t(vote.info)#
vote <- row.names(vote.info)#
vote.info <- data.frame(vote,vote.info,row.names = seq_along(1:length(vote)))#
names(vote.info)<-c("vote.id","date","whipped","vote.info")#
#
data<-drop(data[-1,]) #drop first row (date)#
data<-drop(data[-1,]) #drop second row (whipped)#
data<-drop(data[-1,]) #drop third row (bill type)#
#
votes <- data[,29:dim(data)[2]]#
votes<-as.matrix(votes) #
votes[votes==2] <- 0#
#
partyCode <- recode(data$Party.Name,"'Liberal-Conservative'=1;'Conservative (1867-1942)'=1;'Liberal Party of Canada'=2;'Anti-Confederate'=3;'Independent Conservative'=6;'Independent Liberal'=6;'Independent'=6;'Nationalist'=6;'Nationalist Conservative'=6;'Labour'=3;'McCarthyite'=6;'Patrons of Industry'=3;'Independent Labour'=6;'Unionist'=1;'Unionist (Liberal)'=1;'Laurier Liberal'=2;'Progressive'=22;'United Farmers'=35;'Nationalist Liberal'=2;'United Farmers of Ontario-Labour'=35;'United Farmers of Ontario'=35;'United Farmers of Alberta'=35;'Independent Progressive'=6;'Liberal Progressive'=2;'Co-operative Commonwealth Federation'=33;'Reconstruction Party'=3;'Social Credit'=77;'United Farmers of Ontario-Labour'=35;'United Reform Movement'=3;'Bloc Populaire Canadien'=44;'Unity'=6;'National Government'=1;'Progressive Conservative Party'=1;'Labour Progressive Party'=3;'New Democracy'=6;'Independent Progressive Conservative'=6;'Independent Co-operative Commonwealth Federation'=6;'Liberal Labour Party'=6;'New Party
'=6;'New Democratic Party'=33;'Ralliement Creditiste'=777;'Bloc Quebecois'=44;'Reform Party'=55;'Canadian Alliance'=55;'Conservative Party of Canada'=1;'Independent Reform'=6;'Independent Sovereignist'=6;'Liberal Democrat'=3;'Independent Canadian Alliance'=6;'Independent Bloc Quebecois'=6")#
partyCode <- as.numeric(as.character(partyCode))#
partyName <- recode(partyCode,"'1'='Conservative';'2'='Liberal';'22'='Progressive';'33'='CCF/NDP';'35'='United Farmers';'777'='Ralliement Creditiste (QC)';'77'='Social Credit';'44'='Bloc (QC)';'55'='Reform/Canadian Alliance';'3'='Third';'6'='Independent'")#
partyName <- factor(partyName)#
provinceCode <- recode(data$Province,"'Quebec'=1;'Ontario'=2;'Nova Scotia'=3;'New Brunswick'=4;'Manitoba'=5;'British Columbia'=6; 'Prince Edward Island'=7; 'Northwest Territories'=8; 'Yukon'=9; 'Saskatchewan'=10; 'Alberta'=11; 'Newfoundland and Labrador'=12")  #
provinceCode <- as.numeric(as.character(provinceCode))#
#
legdata1 <- data.frame(data$Province,data$Riding.code,data$Riding,partyName,data$Party.Name,data$Name,data$Id,data$Id.2,data$Parliament.no,data$Term,data$Total.vote,data$Percentage,data$Term,data$Length,data$switch.check,data$Ministry,data$Secretary,data$Change.party,data$Temporary.suspended.caucus,data$Excluded.caucus,data$Quit.caucus)#
data<-data.frame(votes,partyName)#
data.sweet <-data#
#
#Party name loop starts here : #
#
partyNames <- levels(partyName)#
for(f in partyNames){party = f#
#
party_vote.Name<-subset(data,data$partyName==party) ######selecting party, REPLACE PARTY NAME 2/7#
party_vote.Name1<-party_vote.Name#
#
#Yay#
for(i in 1 : length(party_vote.Name))#
{#
party_vote.Name[[i]]=sum(party_vote.Name[[i]]==1,na.rm = TRUE) #calculating all yays in each vote#
}#
#
Name.yes<-party_vote.Name[1,1:dim(party_vote.Name)[2]]  #change to match number of votes, obtain first row only#
Name.yes$partyName <-NULL#
#
#Nay#
#
party_vote.Name<-subset(data,data$partyName==party) #####selecting party, REPLACE PARTY NAME 3/7#
party_vote.Name2<-party_vote.Name                                       #
for(i in 1 : length(party_vote.Name2))#
{#
party_vote.Name2[[i]]=sum(party_vote.Name2[[i]]==0,na.rm = TRUE) #calculating all nays in each vote#
}#
#
Name.no<-party_vote.Name2[1,1:dim(party_vote.Name)[2]]  #change to match number of votes, obtain first row only#
Name.no$partyName <-NULL#
#
#Turnout#
#
party_vote.Name<-subset(data,data$partyName==party)#
party_vote.Name2<-party_vote.Name                                       #
for(i in 1 : length(party_vote.Name2))#
{#
party_vote.Name2[[i]]=sum(party_vote.Name2[[i]]==9 | party_vote.Name2[[i]]==3,na.rm = TRUE) #calculating all absents with paired#
}#
#
Name.absent<-party_vote.Name2[1,1:dim(party_vote.Name)[2]]  #change to match number of votes, obtain first row only#
Name.absent$partyName <-NULL#
#
#Errors#
#
party_vote.Name<-subset(data,data$partyName==party)#
party_vote.Name2<-party_vote.Name                                       #
for(i in 1 : length(party_vote.Name2))#
{#
party_vote.Name2[[i]]=sum(party_vote.Name2[[i]]==4 | party_vote.Name2[[i]]==5 | party_vote.Name2[[i]]==6 | party_vote.Name2[[i]]==7 | party_vote.Name2[[i]]==8 | party_vote.Name2[[i]]==9999,na.rm = TRUE) #calculating all nays in each vote#
}#
#
Name.error<-party_vote.Name2[1,1:dim(party_vote.Name)[2]]  #change to match number of votes, obtain first row only#
Name.error$partyName <-NULL#
#
#total#
#
total<-Name.yes+Name.no#
total2<-Name.yes+Name.no+Name.absent#
#
yes<-Name.yes/total   #
yes<-t(yes) #transpose#
#
no<-Name.no/total  #
no<-t(no) #transpose#
#
turnout <- 1 - (Name.absent/total2)#
turnout <- t(turnout)#
error<-t(Name.error)#
t.total <- t(total)#
t.yes <- t(Name.yes)#
t.no <- t(Name.no)#
t.total2 <- t(total2)#
#
Name.vote.yes<-Name.yes>Name.no  #creating majority variable for yays#
Name.vote.yes<-t(Name.vote.yes)#
Name.vote.no<-Name.no>Name.yes  #creating majority variable for nays#
Name.vote.no<-t(Name.vote.no)#
Name.vote.equ<- Name.yes==Name.no  #creating majority variable for equality#
Name.vote.equ<-t(Name.vote.equ)#
#
Name.vote1<-data.frame(t.total2,t.total,t.yes,t.no,yes,no,turnout,error,Name.vote.yes,Name.vote.no,Name.vote.equ) #creating data.frame with voting and majority#
names(Name.vote1)<-c("total.eligible","total.vote","nb.yes","nb.no","yes","no","turnout","errors","majority.yes","majority.no","equality")#
#
vote.margin <- Name.vote1 #THIS GIVES YOU THE VOTE MARGINS#
#
##################################################################################
#COHESION, voting with plurality of the party#####################################
#ABSTAIN are treated as an option, abstain + vote against are the same############
#CONSEQUENCE you can get a negative score if 2nd + 3rd > 1st, yay + abs > nay...##
##################################################################################
#
vote_yes <-subset(Name.vote1,Name.vote1$majority.yes=='TRUE') #Selecting yay only#
vote_yes <-na.omit(vote_yes)#
yay<-mean(vote_yes$yes)#
yay.total <- yay * nrow(vote_yes)#
yay.total[yay=='NaN'] <-0#
#
vote_no <-subset(Name.vote1,Name.vote1$majority.no=='TRUE')  #Selecting nay only#
vote_no <-na.omit(vote_no)#
nay<-mean(vote_no$no)#
nay.total <- nay * nrow(vote_no)#
nay.total[nay=='NaN'] <-0#
#
vote_equ <-subset(Name.vote1,Name.vote1$equality=='TRUE')  #Selecting equality only#
vote_equ <-na.omit(vote_equ)#
equ<-.5 #Equality must equal .5 (what about three way?)#
equ.total <- equ * nrow(vote_equ)#
#
mean.cohesion <- (yay.total+nay.total+equ.total)/(nrow(vote_yes) + nrow(vote_no) + nrow(vote_equ))#
#
###################
#RICE INDEX########
###################
#
rice.yes <- vote_yes$yes - vote_yes$no #
rice.no <- vote_no$no - vote_no$yes#
vote_equ$equ[vote_equ$equality==TRUE] <- 0#
rice.equ <- vote_equ$equ#
rice <- rbind(matrix(rice.yes),matrix(rice.no),matrix(rice.equ))#
rice.index <- mean(rice)#
#
#ADD RICE BILL#
#
rice.yes.majority <- vote.margin$yes - vote.margin$no #
rice.no.majority <- vote.margin$no - vote.margin$yes #
#
vote.margin.temp <- data.frame(vote.margin, rice.yes.majority,rice.no.majority)#
rice.yes<-vote.margin.temp$rice.yes.majority#
rice.yes[vote.margin.temp$majority.yes == FALSE]<-0#
rice.no<-vote.margin.temp$rice.no.majority#
rice.no[vote.margin.temp$majority.no == FALSE]<-0#
rice.bill <- rice.yes + rice.no#
#
vote.margin <- data.frame(vote.margin,rice.bill)#
#
############################
#INDIVIDUAL LOYALTY SCORES##
#WHAT TO DO EQUALITY = 0 ###
############################
#
majority<-Name.vote1$majority.yes#
majority[Name.vote1$majority.yes ==TRUE] <-1#
majority[Name.vote1$majority.no ==TRUE] <-0#
majority[Name.vote1$equality ==TRUE] <-11#
majority<-as.numeric(majority)#
#
partyName<-data.sweet$partyName #
data<-data.frame(votes,partyName)#
data.file<-subset(data, data$partyName==party)#
datav<-subset(data, data$partyName==party) #
datav[datav==99] <- NA#
datav[datav==9999] <- NA#
datav[datav==8] <- NA#
datav[datav==7] <- NA#
datav[datav==6] <- NA#
datav[datav==5] <- NA#
datav[datav==4] <- NA#
datav[datav==3] <- NA#
q<-length(datav)#
datav<-drop(datav[,-q])#
#
#Getting turnout#
datax<-data.matrix(datav)#
datax[datax>0] <- 1#
total.turnout <- rowSums(datax,na.rm=TRUE)#
#
#Set up data#
datav[datav==9] <- NA #
#
###############################
#THIS IS THE MATRIX OPERATION##
###############################
#
matrix<-matrix(1,nrow=nrow(datav),ncol(datav)) # multiply majority by nb of party member#
#
majority1 <- replicate(nrow(datav),majority)#
majority <- t(majority1)#
#
compare <- datav==majority#
loyalty <- apply(compare,1,mean,na.rm=TRUE)  #this is the formula to get the individual loyalty score, see apply for row.#
total <- compare#
total[total=='FALSE'] <- 1#
total <- rowSums(total,na.rm=TRUE) #get total number of votes#
nb.opp <- total - rowSums(compare,na.rm=TRUE) #get total number of votes against the party#
participation <- total/total.turnout #get participation in the parliament#
#
#Add leg.info#
leg.inform <- subset(legdata1,legdata1$partyName==party) #ALWAYS CHANGE NAME OF DATASETS IN LOOP!!!!!!#
vote.matrix<-cbind(leg.inform,loyalty,total,nb.opp,participation)#
#
parlx <- 1:length(date)#
parlx[parlx>0] <- parlouze#
partys <- 1:length(vote.margin$errors)#
partys[partys>0] <- party#
mean.rices <- 1:length(vote.margin$errors)#
mean.rices[mean.rices>0] <- rice.index#
mean.cohesions <- 1:length(vote.margin$errors)#
mean.cohesions[mean.cohesions>0] <- mean.cohesion#
#
vote.margin <- data.frame(parlx,partys,vote.info,vote.margin,mean.rices,mean.cohesions)#
#
#############################
#RESULTS#####################
#############################
#
print(vote.matrix)#
print(vote.margin)#
print(mean.cohesion)#
print(rice.index)#
#
#Loyalty#
#
for(k in 40){ #change to number of files#
if (k == 1)#
{#
  write.table(vote.matrix, "../../Canadian-House/Analysis/Loyalty-1-40.txt",sep = ";") #
}#
else#
{ #
  write.table(vote.matrix, "../../Canadian-House/Analysis/Loyalty-1-40.txt", append = TRUE, col.names = FALSE,sep = ";")#
}#
}#
#
#vote info#
#
for(k in 40){ #change to number of files#
if (k == 1)#
{#
  write.table(vote.margin, "../../Canadian-House/Analysisy/Vote-1-40.txt",sep = ";") #
}#
else#
{ #
  write.table(vote.margin, "../../Canadian-House/Analysis/Vote-1-40.txt", append = TRUE, col.names = FALSE,sep = ";")#
}#
}#
#
#Party Name loop ends here. #
}#
#
#File loop ends here.#
}#
#
#Saving work#
#
Canada.vote <- read.table("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.txt", header = FALSE, sep = ";")#
Vote.Canada <- data.frame(Canada.vote)#
Vote.Canada<-Vote.Canada[,-1]#
names(Vote.Canada)<-c("province","riding.code","riding.name","party","party.complete","name","id","id2","parliament","term","total.vote","percentage.vote","no.term","occupation","switch.check","ministry","secratary","switch","suspended","excluded","quit","loyalty","total.vote","total.opposition","turnout")#
#
write.csv(Vote.Canada,file = "~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.csv")#
#
Canada.vote <- read.table("~/Dropbox/Canadian-House/Analysis/Vote-1-40.txt", header = FALSE, sep = ";")#
Vote.Canada <- data.frame(Canada.vote)#
Vote.Canada<-Vote.Canada[,-1]#
names(Vote.Canada)<-c("parl","party","vote.id","date","whipped","vote.info","total.eligible","total.vote","nb.yes","nb.no","yes","no","turnout","errors","majority.yes","majority.no","equality","rice.bill","mean.rice","mean.cohesion")#
write.csv(Vote.Canada,file = "~/Dropbox/Canadian-House/Analysis/Vote-1-40.csv")
allloy <- loyalty(Can39)
rm(list=ls())#
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl);library(arm);library(car)#
counter <- 1:40#
parls <- c(paste("Can",counter,sep=""))#
data(list=parls)#
terms <- list(Can1,Can2,Can3,Can4,Can5,Can6,Can7,Can8,#
              Can9,Can10,Can11,Can12,Can13,Can14,Can15,Can16,#
              Can17,Can18,Can19,Can20,Can21,Can22,Can23,Can24,#
              Can25,Can26,Can27,Can28,Can29,Can30,Can31,Can32,#
              Can33,Can34,Can35,Can36,Can37,Can38,Can39,Can40)
allloy <- loyalty(Can39)
allloy
allloy <- loyalty(Can40)
allloy
allloy <- loyaltyall(Can40)
out <- loyaltydata("Liberal",terms,counter)
out
names(out)
?CanParl
?unity
unity(Can40)
?unity
x <- unisty(Can40)
x <- unity(Can40)
x
names(x)
allloy <- loyaltyall(Can40)
allloy <- loyalty(Can40)
allloy
unity(Can40)
unity
list(unity(40))
?unity
data(Can40)
unuty <- unity(Can40)
unuty
data(Can3)#
uni <- unity(Can3)
uni
list(uni)
uni
Can3
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl)#
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl);library(arm);library(car)#
counter <- 1:40#
parls <- c(paste("Can",counter,sep=""))#
data(list=parls)#
terms <- list(Can1,Can2,Can3,Can4,Can5,Can6,Can7,Can8,#
              Can9,Can10,Can11,Can12,Can13,Can14,Can15,Can16,#
              Can17,Can18,Can19,Can20,Can21,Can22,Can23,Can24,#
              Can25,Can26,Can27,Can28,Can29,Can30,Can31,Can32,#
              Can33,Can34,Can35,Can36,Can37,Can38,Can39,Can40)#
#
outdata <- lapply(terms,FUN=unity)#
relterms <-c(1:34,36:40)#
relterms <-c(1:40)#
cons <- list()#
for (i in relterms){#
  cons[[i]] <- do.call("rbind",outdata[[i]]["Conservative"])#
}#
libs <- list()#
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}#
cons <- do.call("rbind",cons)#
libs <- do.call("rbind",libs)#
govinfo <- read.csv("../../Canada_Cohesion//Data/govinfo.csv",header=F)#
colnames(govinfo) <- c("Parliament","govparty","govtype","total.MP")#
cons <- merge(cons,govinfo,by.x="Parlement",by.y="row.names")#
libs <- merge(libs,govinfo,by.x="Parlement",by.y="row.names")
libs
cibs
cons
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl)#
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl);library(arm);library(car)#
counter <- 1:40#
parls <- c(paste("Can",counter,sep=""))#
data(list=parls)#
terms <- list(Can1,Can2,Can3,Can4,Can5,Can6,Can7,Can8,#
              Can9,Can10,Can11,Can12,Can13,Can14,Can15,Can16,#
              Can17,Can18,Can19,Can20,Can21,Can22,Can23,Can24,#
              Can25,Can26,Can27,Can28,Can29,Can30,Can31,Can32,#
              Can33,Can34,Can35,Can36,Can37,Can38,Can39,Can40)#
#
outdata <- lapply(terms,FUN=unity)#
relterms <-c(1:34,36:40)#
relterms <-c(1:40)#
cons <- list()#
for (i in relterms){#
  cons[[i]] <- do.call("rbind",outdata[[i]]["Conservative"])#
}#
libs <- list()#
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}#
cons <- do.call("rbind",cons)#
libs <- do.call("rbind",libs)
libs
outdata
libs <- list()#
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}
libs
libs <- do.call("rbind",libs)
libs
govinfo <- read.csv("../../Canada_Cohesion//Data/govinfo.csv",header=F)
govinfo
terms
outdata <- lapply(terms,FUN=unity)
outdata
names(outdata)
outdata[40]
list(outdata)
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}
outdata[[40]]
libs[[40]] <- do.call("rbind",outdata[[40]]["Liberal"])
libs
libslibs <- list()#
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}
libs <- list()#
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}
libs
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl)#
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl);library(arm);library(car)#
counter <- 1:40#
parls <- c(paste("Can",counter,sep=""))#
data(list=parls)#
terms <- list(Can1,Can2,Can3,Can4,Can5,Can6,Can7,Can8,#
              Can9,Can10,Can11,Can12,Can13,Can14,Can15,Can16,#
              Can17,Can18,Can19,Can20,Can21,Can22,Can23,Can24,#
              Can25,Can26,Can27,Can28,Can29,Can30,Can31,Can32,#
              Can33,Can34,Can35,Can36,Can37,Can38,Can39,Can40)#
#
outdata <- lapply(terms,FUN=unity)#
relterms <-c(1:34,36:40)#
relterms <-c(1:40)#
cons <- list()
libs <- list()
libs
for (i in 1:40){#
  libs[[i]] <- do.call("rbind",outdata[[i]]["Liberal"])#
}
libs[[]]
libs[[i]]
i
rm(list=ls())#
Sys.setlocale(category ="LC_ALL",locale="C")#
library(CanParl);library(arm);library(car)#
counter <- 1:40#
parls <- c(paste("Can",counter,sep=""))#
data(list=parls)#
terms <- list(Can1,Can2,Can3,Can4,Can5,Can6,Can7,Can8,#
              Can9,Can10,Can11,Can12,Can13,Can14,Can15,Can16,#
              Can17,Can18,Can19,Can20,Can21,Can22,Can23,Can24,#
              Can25,Can26,Can27,Can28,Can29,Can30,Can31,Can32,#
              Can33,Can34,Can35,Can36,Can37,Can38,Can39,Can40)
allloy <- loyalty(Can37)
allloy
allout <- read.csv("loyaltyLib.cvs") # Liberal#
alloutc <- read.csv("loyaltyCon.cvs") # Conservative#
allout$parl <- allout$parliament#
allout$parl2 <- allout$parliament^2#
allout$coh <- allout$cohort#
allout$coh2 <- allout$cohort^2#
alloutc$parl <- alloutc$parliament#
alloutc$parl2 <- alloutc$parliament^2#
alloutc$coh <- alloutc$cohort#
alloutc$coh2 <- alloutc$cohort^2#
##########
# french and english speakers#
#liberals#
aggregate(allout$French,by=list(allout$parliament),mean)#
aggregate(allout$French[allout$Quebec==0],by=list(allout$parliament[allout$Quebec==0]),mean)#
mean(allout$French)#
mean(allout$French[allout$Quebec==0])#
# conservatives#
aggregate(alloutc$French,by=list(alloutc$parliament),mean)#
aggregate(alloutc$French[alloutc$Quebec==0],by=list(alloutc$parliament[alloutc$Quebec==0]),mean)#
mean(alloutc$French)#
mean(alloutc$French[alloutc$Quebec==0])#
###################
splittest <- c(5:35)#
dev <-NULL#
devC <- NULL#
empty <- "loyalty.x ~  parliament + I(parliament^2) +split+ parliament:split + I(parliament^2):split"#
emptygov <- "loyalty.y ~  parliament + I(parliament^2) +split+ parliament:split + I(parliament^2):split"#
nomod <- "loyalty.x ~  1"#
model <- "loyalty.x ~  participation.norm + eff.candidates + Total.vote.norm + Cabinet + govparty + govtype + joiner + last + West + Quebec + Maritime + parl+ parl2 + coh + coh2"#
model1 <- "loyalty.x ~  participation.norm + eff.candidates + Total.vote.norm + Cabinet + govparty + joiner + last + West + Quebec + Maritime + parl+ parl2 + coh + coh2"#
fix_model <- "loyalty.x ~  participation.norm + eff.candidates + Total.vote.norm + Cabinet + joiner + last + West + Quebec + Maritime + coh + coh2 + as.factor(parliament)"#
fix_cohort_model <- "loyalty.x ~  participation.norm + eff.candidates + Total.vote.norm + Cabinet + joiner + last + West + Quebec + Maritime +as.factor(cohort) + as.factor(parliament)"#
#
modlist <- c(empty,model,fix_model,fix_cohort_model)#
all <- rbind(allout,alloutc)#
for (i in 1:length(splittest)){#
  all$split <- as.numeric(ifelse(all$parliament<splittest[i],0,1))#
  tmp <- glm(empty,data=all,family=quasibinomial(logit))#
  dev[i] <-  deviance(tmp)#
  cat(splittest[i]," ")#
}#
(Split <- splittest[which.min(dev)])#
#
allout$split <- as.numeric(ifelse(allout$parliament<Split,0,1))#
alloutc$split <- as.numeric(ifelse(alloutc$parliament<Split,0,1))#
#
library(parallel)#
modslib <- mclapply(modlist,glm,data=allout,family=quasibinomial(logit),mc.cores=3)#
modscon <- mclapply(modlist,glm,data=alloutc,family=quasibinomial(logit),mc.cores=3)#
#
modslib[[5]] <- glm(model1,data=allout[allout$parliament<Split,],family=quasibinomial(logit))#
modslib[[6]]<- glm(model,data=allout[allout$parliament>=Split,],family=quasibinomial(logit))#
#
modscon[[5]] <- glm(model1,data=alloutc[alloutc$parliament<Split,],family=quasibinomial(logit))#
modscon[[6]]<- glm(model,data=alloutc[alloutc$parliament>=Split,],family=quasibinomial(logit))#
(n.ons <- lapply(modslib,function(x) length(x$y)))#
(n.ons <- lapply(modscon,function(x) length(x$y)))#
# robust standard errors#
library(AER)#
#
libres <- mclapply(modslib,coeftest,df=Inf,vcov=vcovHAC,mc.cores=3)#
conres <- mclapply(modscon,coeftest,df=Inf,vcov=vcovHAC,mc.cores=3)#
#
libres#
conres#
########## make tables#
coefcol <- function(indata){#
  coefs <- round(indata[,1],3)#
  st.errors <- round(indata[,2],3)#
  stars <- ifelse(abs(indata[,1])>1.96*indata[,2],"*","")#
  res <- paste(coefs,stars,sep="")#
  st.errs <- paste("(",st.errors,")",sep="")#
  st.errs <- ifelse(st.errs=="(0)","(0.00)",st.errs)#
  res <- ifelse(res=="0","0.00",res)#
  names(res) <- rownames(indata)#
  names(st.errs) <- rownames(indata)#
  out <- cbind(res,st.errs)#
  return(out)#
}#
test <- lapply(libres,coefcol)#
testcon <- lapply(conres,coefcol)#
outtest <- merge(test[[2]],test[[4]],by="row.names",all=T,sort=F)#
outtest <- merge(outtest,test[[5]],by.x="Row.names",by.y="row.names",all=T,sort=F)#
outtest <- merge(outtest,test[[6]],by.x="Row.names",by.y="row.names",all=T,sort=F)#
outtest <- outtest[-c(17:95),] # drops fixed effects#
outtest#
#
outtestcon <- merge(testcon[[2]],testcon[[4]],by="row.names",all=T,sort=F)#
outtestcon <- merge(outtestcon,testcon[[5]],by.x="Row.names",by.y="row.names",all=T,sort=F)#
outtestcon <- merge(outtestcon,testcon[[6]],by.x="Row.names",by.y="row.names",all=T,sort=F)#
outtestcon <- outtestcon[-c(17:95),] # drops fixed effects#
outtestcon#
out <- merge(outtest,outtestcon,by="Row.names",sort=F)#
longres <- function(wideres){#
  odd <- seq(from=1,to=2*nrow(wideres),by=2)#
  even <- seq(from=2,to=2*nrow(wideres),by=2)#
  mods <- seq(from=2,to=ncol(wideres),by=2)#
  resmat <- matrix(NA,nrow=2*nrow(wideres),ncol=(ncol(wideres)+1)/2)#
  rescols <- seq(from=2,to=ncol(resmat))#
  for (i in mods){#
    tmp <- rep(NA,2*nrow(wideres))#
    tmp[odd] <- as.character(wideres[,i])#
    tmp[even] <- as.character(wideres[,i+1])#
    j <- rescols[i/2]#
    resmat[,j] <- tmp#
  }#
  resmat[odd,1] <- wideres[,1] #
  return(resmat)#
}#
loyaltyres <- longres(out)#
colnames(loyaltyres) <- c("variables","base (lib)","fe (lib)","before (lib)","after (lib)","base (con)","fe (con)","before (con)","after (con)")#
library(xtable)#
print(xtable(loyaltyres,caption="Fraction Logit: Voting Loyalty",label="tab:loyalty"),include.rownames=F,size="tiny",file="Table-1.tex",tabular.environment="longtable",floating=FALSE)
library(CanParl)
loyalty
nstall.packages("~/Dropbox/CanadaLong/CanParl_0.064.tar.gz", repos = NULL, type = "source")
install.packages("~/Dropbox/CanadaLong/CanParl_0.064.tar.gz", repos = NULL, type = "source")
Loyalty analysis, Canadian House of Commons, 41st Parliament.#
#Jean-François Godbout, Département de science politique, Université de Montréal#
#This version: June 18, 2015#
#Data from: http://www.parl.gc.ca/Parliamentarians/en/members?parliament=41&view=ListAll#
#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/Loyalty-41.csv",recursive=TRUE)#
#
library(foreign)#
data1 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s1.csv")#
data2 <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-vote-41-s2.csv")#
data <- rbind(data1,data2)#
#
#Add party info to data#
party <- read.csv("~/Dropbox/Canadian-House/House-41/data/MP-party.csv")#
#
data$name <- paste(data$last.name,data$first.name)#
party$name <- paste(party$Last.Name,party$First.Name)#
#
pp <- data.frame(party$name,party$Political.Affiliation)#
colnames(pp) <- c("name","party")#
merge <- merge(data,pp,by.x="name")#
#
vote <- merge#
#
#Change the date format#
#
vote$date <- as.character(vote$date)#
x <- read.table(text=as.character(vote$date), sep=' ', fill=TRUE, as.is=TRUE)#
vote$date.vote <- x$V1#
#
#Get rid of party switchers#
#
#extract members#
vote$name <- factor(vote$name)#
dd <- data.frame(vote)#
dd$date.vote <- as.Date(dd$date.vote, "%m/%d/%Y")#
#
##2011.12.05--2013-06-09 Goldring Peter Independent Conservative (from Conservative)#
#2013.06.10 Goldring Peter, Conservative#
#
dd$party1 <- ifelse(dd$date.vote < as.Date("2011-12-05") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2011-12-04") & dd$date.vote < as.Date("2013-06-09") & dd$name=="Goldring Peter","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-09") & dd$name=="Goldring Peter","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
#
#2012.01.10 St-Denis, Lise Liberal (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-01-10") & dd$name=="St-Denis Lise","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-01-09") & dd$name=="St-Denis Lise","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2012.04.23 Hyer, Bruse, Independent (from NDP)#
#2013.12.13 Hyer, Bruce, Green Party (from Independent)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-04-23") & dd$name=="Hyer Bruce","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-04-22") & dd$date.vote < as.Date("2013-12-13") & dd$name=="Hyer Bruce","Independent","NA")#
dd$party <- ifelse(dd$party1=="Indepenent","Independent",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-12-12") & dd$name=="Hyer Bruce","Green Party","NA")#
dd$party <- ifelse(dd$party1=="Green Party","Green Party",as.character(dd$party))#
#
#2013.02.28 Patry, Claude, Bloc Quebecois (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2012-02-28") & dd$name=="Patry Claude","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2012-02-27") & dd$name=="Patry Claude","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
#
#2013.06.06 Rathberger, Brent, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-06-06") & dd$name=="Rathgeber Brent","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-06-05") & dd$name=="Rathgeber Brent","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.12 Mourani, Maria, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-12") & dd$name=="Mourani Maria","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-11") & dd$name=="Mourani Maria","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.09.30 Del Mastro, Dean, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-09-30") & dd$name=="Del Mastro Dean","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-09-29") & dd$name=="Del Mastro Dean","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.06.06 Perreault, Manon, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-06-06") & dd$name=="Perreault Manon","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-06-05") & dd$name=="Perreault Manon","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.12-2014.10.20 Fortin, Jean-François, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-12") & dd$name=="Fortin Jean-Fran\xe7ois","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-11") & dd$date.vote < as.Date("2013-10-21") & dd$name=="Fortin Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.20 Hassainia, Sana, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-20") & dd$name=="Hassainia Sana","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-19") & dd$name=="Hassainia Sana","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.08.25 Bellavance, André, Independent (from Bloc)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-08-25") & dd$name=="Bellavance Andr\xe9","Bloc Qu\xe9b\xe9cois","NA")#
dd$party <- ifelse(dd$party1=="Bloc Qu\xe9b\xe9cois","Bloc Qu\xe9b\xe9cois",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-08-24") & dd$name=="Bellavance Andr\xe9","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.10.21 Larose, Jean-François, Force et Démocratie (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-10-21") & dd$name=="Larose Jean-Fran\xe7ois","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-10-20") & dd$name=="Larose Jean-Fran\xe7ois","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Pacetti, Massimo, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Pacetti Massimo","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Pacetti Massimo","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2014.11.05 Andrews, Scott, Independent (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2014-11-05") & dd$name=="Andrews Scott","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2014-11-04") & dd$name=="Andrews Scott","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.01.05 Thibeault, Glenn, Independent (from NDP)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-01-05") & dd$name=="Thibeault Glenn","NDP","NA")#
dd$party <- ifelse(dd$party1=="NDP","NDP",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-01-04") & dd$name=="Thibeault Glenn","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2015.02.09 Adams, Eve, Liberal (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-02-09") & dd$name=="Adams Eve","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-02-08") & dd$name=="Adams Eve","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
#
#2015.03.31 Lunney, James, Independent (from Conservative)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2015-03-30") & dd$name=="Lunney James","Conservative","NA")#
dd$party <- ifelse(dd$party1=="Conservative","Conservative",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2015-03-30") & dd$name=="Lunney James","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#2013.05.10 - 2013.08.26 Marc Harb (from Liberal Party)#
dd$party1 <- ifelse(dd$date.vote < as.Date("2013-05-10") & dd$name=="Harb Marc","Liberal","NA")#
dd$party <- ifelse(dd$party1=="Liberal","Liberal",as.character(dd$party))#
dd$party1 <- ifelse(dd$date.vote > as.Date("2013-05-09") & dd$name=="Harb Marc","Independent","NA")#
dd$party <- ifelse(dd$party1=="Independent","Independent",as.character(dd$party))#
#
#Compute individual vote#
#
vote <- dd#
#
vote$vote.c <- ifelse(vote$vote=='Nay',0,ifelse(vote$vote=='Yea',1,ifelse(vote$vote=='Yea/Nay',99,ifelse(vote$vote=='Paired',8,NA))))#
#
#Create vote id variable#
#
vote$vote.id <-  paste(vote$session,vote$vote.no,sep = "v")#
#
vote$vote.id <- paste("s",vote$vote.id,sep = "")#
#
vote$vote.id <- factor(vote$vote.id)#
#
dd <- data.frame(vote)#
#
#Get rid of duplicates#
d <- dd[,-2]#
de <- d[,-17]#
df <- unique(de)#
#
####################################################
#1. loop for majority support in all three parties##
####################################################
#
#1.1 Conservative#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Conservative')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("conservative")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.2 Liberal#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='Liberal')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("liberal")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
liberal.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(liberal.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#1.3 NDP#
for(j in levels(df$vote.id)){ #change to number of files#
#
x <- subset(df,df$vote.id==j & df$party=='NDP')#
yay <- sum(ifelse(x$vote.c==1,1,0))#
nay <- sum(ifelse(x$vote.c==0,1,0))#
party <- c("ndp")#
temp <- data.frame(j,party,yay,nay)#
#
if (j == 's1v1')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.margin <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.margin) <- c("burn","vote.id","party","yay","nay")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
#select three parties for analysis#
#
cons <- subset(dd,dd$party=='Conservative')#
lib <- subset(dd,dd$party=='Liberal')#
ndp <- subset(dd,dd$party=='NDP')#
#
##############################################
#2. loop individual mp to get loyalty scores##
##############################################
#
#2.1 Conservative#
#
#match individual MP with party position#
#
cons.margin$party.position <- ifelse(cons.margin$yay > cons.margin$nay,1,ifelse(cons.margin$yay < cons.margin$nay,0,NA))#
merge.cons <- merge(cons,cons.margin,"vote.id")#
#
merge.cons$support <- ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 1, 1,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 0, 1,ifelse(merge.cons$vote.c == 1 & merge.cons$party.position == 0, 0,ifelse(merge.cons$vote.c == 0 & merge.cons$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.cons <- subset(merge.cons,merge.cons$session!=3)#
#
#get rid of other party members#
merge.cons$name <- factor(merge.cons$name)#
#
#start individual loop#
for(j in levels(merge.cons$name)){ #change to number of files#
#
x <- subset(merge.cons,merge.cons$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("conservative")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
cons.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(cons.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
cons.loyalty[order(cons.loyalty$loyalty),]#
#
#Rice index conservative#
#
cons.margin$rice <- abs(cons.margin$yay - cons.margin$nay)/(cons.margin$yay + cons.margin$nay)#
cons.margin[order(cons.margin$rice),]#
#
#2.1 Liberal#
#
#match individual MP with party position#
#
lib.margin <- liberal.margin#
#
lib.margin$party.position <- ifelse(lib.margin$yay > lib.margin$nay,1,ifelse(lib.margin$yay < lib.margin$nay,0,NA))#
merge.lib <- merge(lib,lib.margin,"vote.id")#
#
merge.lib$support <- ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 1, 1,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 0, 1,ifelse(merge.lib$vote.c == 1 & merge.lib$party.position == 0, 0,ifelse(merge.lib$vote.c == 0 & merge.lib$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.lib <- subset(merge.lib,merge.lib$session!=3)#
#
#get rid of other party members#
merge.lib$name <- factor(merge.lib$name)#
#
#start individual loop#
for(j in levels(merge.lib$name)){ #change to number of files#
#
x <- subset(merge.lib,merge.lib$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("liberal")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
lib.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(lib.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
lib.loyalty[order(lib.loyalty$loyalty),]#
#
#Rice index liberal#
#
lib.margin$rice <- abs(lib.margin$yay - lib.margin$nay)/(lib.margin$yay + lib.margin$nay)#
lib.margin[order(lib.margin$rice),]#
#
#3.1 NDP#
#
#match individual MP with party position#
#
ndp.margin$party.position <- ifelse(ndp.margin$yay > ndp.margin$nay,1,ifelse(ndp.margin$yay < ndp.margin$nay,0,NA))#
merge.ndp <- merge(ndp,ndp.margin,"vote.id")#
#
merge.ndp$support <- ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 1, 1,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 0, 1,ifelse(merge.ndp$vote.c == 1 & merge.ndp$party.position == 0, 0,ifelse(merge.ndp$vote.c == 0 & merge.ndp$party.position == 1, 0,NA))))#
#
#delete session 3, for some reasons Parliament website has errors (no third session, but included in the data).#
#
merge.ndp <- subset(merge.ndp,merge.ndp$session!=3)#
#
#get rid of other party members#
merge.ndp$name <- factor(merge.ndp$name)#
#
#start individual loop#
for(j in levels(merge.ndp$name)){ #change to number of files#
#
x <- subset(merge.ndp,merge.ndp$name==j)#
loyalty <- mean(x$support,na.rm = TRUE)#
no.opposition <- sum(ifelse(x$support==0,1,0),na.rm=TRUE)#
party <- c("ndp")#
temp <- data.frame(j,party,loyalty,no.opposition)#
#
if (j == 'Ablonczy Diane')#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt",col.names = FALSE,sep = ";")#
  }#
else#
  {#
    tab <- write.table(temp,"~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", append = TRUE, col.names = FALSE,sep = ";")#
  }#
#
}#
#
ndp.loyalty <- read.table("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", header = FALSE, sep = ";")#
colnames(ndp.loyalty) <- c("burn","name","party","loyalty","no.opposition")#
file.remove("~/Dropbox/Canadian-House/House-41/Analysis/vote-info.txt", recursive=TRUE)#
#
ndp.loyalty[order(ndp.loyalty$loyalty),]#
#
#Rice index ndp#
#
ndp.margin$rice <- abs(ndp.margin$yay - ndp.margin$nay)/(ndp.margin$yay + ndp.margin$nay)#
ndp.margin[order(ndp.margin$rice),]#
#
#combine#
#
data.full <- rbind(cons.loyalty,lib.loyalty,ndp.loyalty)#
#
write.csv(data.full,file = "~/Dropbox/Canadian-House/House-41/Analysis/Loyalty-41.csv")
data.fukk
data.full[1:10,]
ons.loyalty
cons.loyalty
ndp.loyalty
ndp.margin
ndp.margin$yay + ndp.margin$nay
sum(ndp.margin$yay + ndp.margin$nay)
sum(lib.margin$yay + lib.margin$nay)
sum(cons.margin$yay + cons.margin$nay))
sum(cons.margin$yay + cons.margin$nay)
ndp1 <- sum(ndp.margin$yay + ndp.margin$nay)#
lib1 <- sum(lib.margin$yay + lib.margin$nay)#
cons1 <- sum(cons.margin$yay + cons.margin$nay)
ndp1+lib1+cons1
lib.margin$yay + lib.margin$nay
cons.margin$yay + cons.margin$nay
ndp1 <- ndp.margin$yay + ndp.margin$nay#
lib1 <- lib.margin$yay + lib.margin$nay#
cons1 <- cons.margin$yay + cons.margin$nay
ndp1
ndp1 <- ndp.margin$yay + ndp.margin$nay#
lib1 <- lib.margin$yay + lib.margin$nay#
cons1 <- cons.margin$yay + cons.margin$nay
data.full
data.full$no.opposition
sum(data.full$no.opposition)
data <- read.cv("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.csv")
data <- read.csv("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.csv")
names(data)
data
data <- read.csv("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.csv"
)
data
data <- read.csv("~/Dropbox/Canadian-House/Analysis/Loyalty-1-40.csv"
)
names(data)
setwd(~/Dropbox/Canada_Cohesion/Letter-Editor-Error/Replication/UnityAbstentions.r', chdir = TRUE))
setwd("~/Dropbox/Canada_Cohesion/Letter-Editor-Error/Replication/")
source("Loyal.r")
source("Loyal.r")
